Skip to content

:fa-brands-github: Show source on GitHub

Node to include the text of other Markdown files / MkNodes.

Description

This node only keeps a reference to given target and resolves it when needed. Target can either be an URL or any other fsspec protocol path to a markdown file, a file system path to a markdown file, or another MkNode.

Example: Include from URL

Jinja

{{ "https://raw.githubusercontent.com/phil65/mknodes/main/README.md" | MkInclude }}

Python

MkInclude('https://raw.githubusercontent.com/phil65/mknodes/main/README.md')

MkNodes

Don't write docs. Code them.

PyPI License Package status Daily downloads Weekly downloads Monthly downloads Distribution format Wheel availability Python version Implementation Releases Github Contributors Github Discussions Github Forks Github Issues Github Issues Github Watchers Github Stars Github Repository size Github last commit Github release date Github language count Github commits this week Github commits this month Github commits this year Package status PyUp

Read the completely coded documentation!

The documentation is available in six variants:

Variant Engine / flavour
Zensical --engine zensical (default), Material flavour
MkDocs --engine mkdocs, Material for MkDocs
mdBook --engine mdbook, mdBook flavour
Sphinx --engine sphinx, MyST flavour (Furo theme)
Docsify --engine docsify, GitHub flavour, rendered in the browser
GitHub Markdown --engine markdown --flavor github, rendered by GitHub itself

They are all generated from the same build function (mknodes.manual.root:Build.build): the node tree is written once and each engine gets the Markdown flavour it understands.

MkNodes lets you build documentation from Python objects instead of hand-written Markdown. Pages, navigation sections and content blocks are nodes that render to Material for MkDocs-flavored Markdown and know which Markdown extensions, CSS and JavaScript they need. Many nodes pull their content directly from your project (pyproject.toml, git history, installed packages, docstrings), so the docs stay in sync with the code.

Installation

pip install mknodes

MkNodes includes a site generator (mknodes build / serve / generate) that builds websites with Zensical (default), MkDocs (pip install mknodes[mkdocs]), mdBook (the mdbook binary), Sphinx with MyST (pip install mknodes[sphinx]) or Docsify (no build step), or writes plain Markdown. MkDocs is not required. (The generator used to be the separate mkdocs-mknodes package.)

Quickstart

import mknodes as mk

page = mk.MkPage("Overview")
page += mk.MkHeader("My project", level=1)
page += mk.MkAdmonition("Generated with MkNodes!", typ="tip", title="Note")
page += mk.MkTable({"Feature": ["Nodes", "Templating"], "Status": ["yes", "yes"]})
page += mk.MkCode("print('hello')", language="py")

print(page.to_markdown_sync())  # async code: `await page.to_markdown()`
print(page.to_markdown_sync(flavor="github"))  # GitHub flavour (READMEs)

Features

  • 75+ nodes: admonitions, tabs, tables, code blocks, diagrams, cards, timelines, badges, ...
  • Project-aware nodes: MkChangelog, MkInstallGuide, MkDependencyTable, MkLicense, MkShields, MkClassDiagram, ... build their content from your package and repository.
  • Navigation trees: MkNav sections with decorator-based routing, API docs via MkDoc, CLI docs via MkCliNav, or parse an existing SUMMARY.md / folder.
  • Jinja templating: every node is also a Jinja filter, e.g. {{ "Inlined text" | MkAdmonition(inline="left") }}.
  • Resource tracking: required Markdown extensions, CSS and JS are collected per page.
  • Markdown extensions: /// mknodes blocks and executable Python code blocks (mknodes.mdext).

Build a website tree

import mknodes as mk


def build(root: mk.MkNav, theme: mk.Theme) -> None:
    @root.route.page("Home", is_index=True)
    def _(page: mk.MkPage):
        page += mk.MkHeader("Welcome", level=1)
        page += mk.MkInstallGuide()

    @root.route.page("Changelog", icon="format-list-group")
    def _(page: mk.MkPage):
        page += mk.MkChangelog()

Configure it in pyproject.toml and build the website (run inside your project's repository):

[tool.mknodes]
build-fn = "mypackage.docs:build"
mknodes build                      # pages + mkdocs.generated.yml, then `zensical build`
mknodes serve                      # live preview
mknodes build --engine mkdocs      # same input, built with MkDocs + Material
mknodes build --engine mdbook      # book/ (book.toml, src/SUMMARY.md), then `mdbook build`
mknodes build --engine sphinx      # sphinx/ (conf.py, MyST pages, toctrees), then `sphinx-build`
mknodes build --engine docsify     # site/ (index.html, _sidebar.md, pages): no build step
mknodes generate --engine markdown --flavor github --generated-dir out  # plain Markdown

Each engine renders its own Markdown flavour (material for Zensical / MkDocs, mdbook, myst for Sphinx, github for Docsify and plain Markdown): admonitions, tabs, code blocks and API docs degrade to what the target understands. See Site engines.

Without a build function, a default website gets generated (README, API docs, CLI docs, development pages). Build functions are called as build_fn(root=..., theme=..., **kwargs) (sync or async) and populate root.

The CLI also renders Jinja-enhanced Markdown: mknodes render -i template.md -o output.md and mknodes render-folder -i src_docs -o docs.

Markdown extensions

import markdown

from mknodes.mdext.execute_ext import get_custom_fences

md = markdown.Markdown(
    extensions=["pymdownx.superfences", "mknodes.mdext"],
    extension_configs={"pymdownx.superfences": {"custom_fences": get_custom_fences()}},
)
/// mknodes
{{ "Dynamic Header" | MkHeader(level=2) }}
///

```python exec="true"
print("**Generated at build time!**")
```

Use session="name" on exec blocks to share variables between them. See src/mknodes/mdext/README.md for all options.

Documentation

The documentation is itself built with MkNodes (see src/mknodes/manual/) and shows every node with examples.

# MkNodes

#### Don't write docs. Code them.

[![PyPI License](https://img.shields.io/pypi/l/mknodes.svg)](https://pypi.org/project/mknodes/)
[![Package status](https://img.shields.io/pypi/status/mknodes.svg)](https://pypi.org/project/mknodes/)
[![Daily downloads](https://img.shields.io/pypi/dd/mknodes.svg)](https://pypi.org/project/mknodes/)
[![Weekly downloads](https://img.shields.io/pypi/dw/mknodes.svg)](https://pypi.org/project/mknodes/)
[![Monthly downloads](https://img.shields.io/pypi/dm/mknodes.svg)](https://pypi.org/project/mknodes/)
[![Distribution format](https://img.shields.io/pypi/format/mknodes.svg)](https://pypi.org/project/mknodes/)
[![Wheel availability](https://img.shields.io/pypi/wheel/mknodes.svg)](https://pypi.org/project/mknodes/)
[![Python version](https://img.shields.io/pypi/pyversions/mknodes.svg)](https://pypi.org/project/mknodes/)
[![Implementation](https://img.shields.io/pypi/implementation/mknodes.svg)](https://pypi.org/project/mknodes/)
[![Releases](https://img.shields.io/github/downloads/phil65/mknodes/total.svg)](https://github.com/phil65/mknodes/releases)
[![Github Contributors](https://img.shields.io/github/contributors/phil65/mknodes)](https://github.com/phil65/mknodes/graphs/contributors)
[![Github Discussions](https://img.shields.io/github/discussions/phil65/mknodes)](https://github.com/phil65/mknodes/discussions)
[![Github Forks](https://img.shields.io/github/forks/phil65/mknodes)](https://github.com/phil65/mknodes/forks)
[![Github Issues](https://img.shields.io/github/issues/phil65/mknodes)](https://github.com/phil65/mknodes/issues)
[![Github Issues](https://img.shields.io/github/issues-pr/phil65/mknodes)](https://github.com/phil65/mknodes/pulls)
[![Github Watchers](https://img.shields.io/github/watchers/phil65/mknodes)](https://github.com/phil65/mknodes/watchers)
[![Github Stars](https://img.shields.io/github/stars/phil65/mknodes)](https://github.com/phil65/mknodes/stars)
[![Github Repository size](https://img.shields.io/github/repo-size/phil65/mknodes)](https://github.com/phil65/mknodes)
[![Github last commit](https://img.shields.io/github/last-commit/phil65/mknodes)](https://github.com/phil65/mknodes/commits)
[![Github release date](https://img.shields.io/github/release-date/phil65/mknodes)](https://github.com/phil65/mknodes/releases)
[![Github language count](https://img.shields.io/github/languages/count/phil65/mknodes)](https://github.com/phil65/mknodes)
[![Github commits this week](https://img.shields.io/github/commit-activity/w/phil65/mknodes)](https://github.com/phil65/mknodes)
[![Github commits this month](https://img.shields.io/github/commit-activity/m/phil65/mknodes)](https://github.com/phil65/mknodes)
[![Github commits this year](https://img.shields.io/github/commit-activity/y/phil65/mknodes)](https://github.com/phil65/mknodes)
[![Package status](https://codecov.io/gh/phil65/mknodes/branch/main/graph/badge.svg)](https://codecov.io/gh/phil65/mknodes/)
[![PyUp](https://pyup.io/repos/github/phil65/mknodes/shield.svg)](https://pyup.io/repos/github/phil65/mknodes/)

[Read the completely coded documentation!](https://phil65.github.io/mknodes/)

The documentation is available in six variants:

| Variant | Engine / flavour |
| --- | --- |
| [Zensical](https://phil65.github.io/mknodes/) | `--engine zensical` (default), Material flavour |
| [MkDocs](https://phil65.github.io/mknodes/mkdocs/) | `--engine mkdocs`, Material for MkDocs |
| [mdBook](https://phil65.github.io/mknodes/mdbook/) | `--engine mdbook`, mdBook flavour |
| [Sphinx](https://phil65.github.io/mknodes/sphinx/) | `--engine sphinx`, MyST flavour (Furo theme) |
| [Docsify](https://phil65.github.io/mknodes/docsify/) | `--engine docsify`, GitHub flavour, rendered in the browser |
| [GitHub Markdown](https://github.com/phil65/mknodes/tree/docs-github) | `--engine markdown --flavor github`, rendered by GitHub itself |

They are all generated from the same build function (`mknodes.manual.root:Build.build`):
the node tree is written once and each engine gets the Markdown flavour it understands.

MkNodes lets you build documentation from Python objects instead of hand-written Markdown.
Pages, navigation sections and content blocks are *nodes* that render to
[Material for MkDocs](https://squidfunk.github.io/mkdocs-material/)-flavored Markdown and know
which Markdown extensions, CSS and JavaScript they need. Many nodes pull their content directly
from your project (pyproject.toml, git history, installed packages, docstrings), so the docs stay
in sync with the code.

## Installation

```bash
pip install mknodes
```

MkNodes includes a site generator (`mknodes build` / `serve` / `generate`) that builds
websites with [Zensical](https://zensical.org) (default), [MkDocs](https://www.mkdocs.org)
(`pip install mknodes[mkdocs]`), [mdBook](https://rust-lang.github.io/mdBook/) (the `mdbook`
binary), [Sphinx](https://www.sphinx-doc.org) with MyST (`pip install mknodes[sphinx]`) or
[Docsify](https://docsify.js.org) (no build step), or writes plain Markdown. MkDocs is not required. (The generator used to be the
separate `mkdocs-mknodes` package.)

## Quickstart

```py
import mknodes as mk

page = mk.MkPage("Overview")
page += mk.MkHeader("My project", level=1)
page += mk.MkAdmonition("Generated with MkNodes!", typ="tip", title="Note")
page += mk.MkTable({"Feature": ["Nodes", "Templating"], "Status": ["yes", "yes"]})
page += mk.MkCode("print('hello')", language="py")

print(page.to_markdown_sync())  # async code: `await page.to_markdown()`
print(page.to_markdown_sync(flavor="github"))  # GitHub flavour (READMEs)
```

## Features

- **75+ nodes**: admonitions, tabs, tables, code blocks, diagrams, cards, timelines, badges, ...
- **Project-aware nodes**: `MkChangelog`, `MkInstallGuide`, `MkDependencyTable`, `MkLicense`,
  `MkShields`, `MkClassDiagram`, ... build their content from your package and repository.
- **Navigation trees**: `MkNav` sections with decorator-based routing, API docs via `MkDoc`,
  CLI docs via `MkCliNav`, or parse an existing `SUMMARY.md` / folder.
- **Jinja templating**: every node is also a Jinja filter, e.g.
  `{{ "Inlined text" | MkAdmonition(inline="left") }}`.
- **Resource tracking**: required Markdown extensions, CSS and JS are collected per page.
- **Markdown extensions**: `/// mknodes` blocks and executable Python code blocks
  (`mknodes.mdext`).

### Build a website tree

```py
import mknodes as mk


def build(root: mk.MkNav, theme: mk.Theme) -> None:
    @root.route.page("Home", is_index=True)
    def _(page: mk.MkPage):
        page += mk.MkHeader("Welcome", level=1)
        page += mk.MkInstallGuide()

    @root.route.page("Changelog", icon="format-list-group")
    def _(page: mk.MkPage):
        page += mk.MkChangelog()
```

Configure it in `pyproject.toml` and build the website (run inside your project's repository):

```toml
[tool.mknodes]
build-fn = "mypackage.docs:build"
```

```bash
mknodes build                      # pages + mkdocs.generated.yml, then `zensical build`
mknodes serve                      # live preview
mknodes build --engine mkdocs      # same input, built with MkDocs + Material
mknodes build --engine mdbook      # book/ (book.toml, src/SUMMARY.md), then `mdbook build`
mknodes build --engine sphinx      # sphinx/ (conf.py, MyST pages, toctrees), then `sphinx-build`
mknodes build --engine docsify     # site/ (index.html, _sidebar.md, pages): no build step
mknodes generate --engine markdown --flavor github --generated-dir out  # plain Markdown
```

Each engine renders its own Markdown flavour (`material` for Zensical / MkDocs, `mdbook`,
`myst` for Sphinx, `github` for Docsify and plain Markdown): admonitions, tabs, code blocks and API docs degrade to what the
target understands. See [Site engines](https://phil65.github.io/mknodes/Site%20generator/site_engines/).

Without a build function, a default website gets generated (README, API docs, CLI docs,
development pages). Build functions are called as `build_fn(root=..., theme=..., **kwargs)`
(sync or async) and populate `root`.

The CLI also renders Jinja-enhanced Markdown: `mknodes render -i template.md -o output.md`
and `mknodes render-folder -i src_docs -o docs`.

### Markdown extensions

```py
import markdown

from mknodes.mdext.execute_ext import get_custom_fences

md = markdown.Markdown(
    extensions=["pymdownx.superfences", "mknodes.mdext"],
    extension_configs={"pymdownx.superfences": {"custom_fences": get_custom_fences()}},
)
```

````markdown
/// mknodes
{{ "Dynamic Header" | MkHeader(level=2) }}
///

```python exec="true"
print("**Generated at build time!**")
```
````

Use `session="name"` on `exec` blocks to share variables between them. See
[`src/mknodes/mdext/README.md`](https://github.com/phil65/mknodes/blob/main/src/mknodes/mdext/README.md) for all options.

## Documentation

The [documentation](https://phil65.github.io/mknodes/) is itself built with MkNodes
(see `src/mknodes/manual/`) and shows every node with examples.
<h1 id="mknodes">MkNodes</h1>
<h4 id="dont-write-docs-code-them">Don't write docs. Code them.</h4>
<p><a href="https://pypi.org/project/mknodes/"><img alt="PyPI License" src="https://img.shields.io/pypi/l/mknodes.svg"></a>
<a href="https://pypi.org/project/mknodes/"><img alt="Package status" src="https://img.shields.io/pypi/status/mknodes.svg"></a>
<a href="https://pypi.org/project/mknodes/"><img alt="Daily downloads" src="https://img.shields.io/pypi/dd/mknodes.svg"></a>
<a href="https://pypi.org/project/mknodes/"><img alt="Weekly downloads" src="https://img.shields.io/pypi/dw/mknodes.svg"></a>
<a href="https://pypi.org/project/mknodes/"><img alt="Monthly downloads" src="https://img.shields.io/pypi/dm/mknodes.svg"></a>
<a href="https://pypi.org/project/mknodes/"><img alt="Distribution format" src="https://img.shields.io/pypi/format/mknodes.svg"></a>
<a href="https://pypi.org/project/mknodes/"><img alt="Wheel availability" src="https://img.shields.io/pypi/wheel/mknodes.svg"></a>
<a href="https://pypi.org/project/mknodes/"><img alt="Python version" src="https://img.shields.io/pypi/pyversions/mknodes.svg"></a>
<a href="https://pypi.org/project/mknodes/"><img alt="Implementation" src="https://img.shields.io/pypi/implementation/mknodes.svg"></a>
<a href="https://github.com/phil65/mknodes/releases"><img alt="Releases" src="https://img.shields.io/github/downloads/phil65/mknodes/total.svg"></a>
<a href="https://github.com/phil65/mknodes/graphs/contributors"><img alt="Github Contributors" src="https://img.shields.io/github/contributors/phil65/mknodes"></a>
<a href="https://github.com/phil65/mknodes/discussions"><img alt="Github Discussions" src="https://img.shields.io/github/discussions/phil65/mknodes"></a>
<a href="https://github.com/phil65/mknodes/forks"><img alt="Github Forks" src="https://img.shields.io/github/forks/phil65/mknodes"></a>
<a href="https://github.com/phil65/mknodes/issues"><img alt="Github Issues" src="https://img.shields.io/github/issues/phil65/mknodes"></a>
<a href="https://github.com/phil65/mknodes/pulls"><img alt="Github Issues" src="https://img.shields.io/github/issues-pr/phil65/mknodes"></a>
<a href="https://github.com/phil65/mknodes/watchers"><img alt="Github Watchers" src="https://img.shields.io/github/watchers/phil65/mknodes"></a>
<a href="https://github.com/phil65/mknodes/stars"><img alt="Github Stars" src="https://img.shields.io/github/stars/phil65/mknodes"></a>
<a href="https://github.com/phil65/mknodes"><img alt="Github Repository size" src="https://img.shields.io/github/repo-size/phil65/mknodes"></a>
<a href="https://github.com/phil65/mknodes/commits"><img alt="Github last commit" src="https://img.shields.io/github/last-commit/phil65/mknodes"></a>
<a href="https://github.com/phil65/mknodes/releases"><img alt="Github release date" src="https://img.shields.io/github/release-date/phil65/mknodes"></a>
<a href="https://github.com/phil65/mknodes"><img alt="Github language count" src="https://img.shields.io/github/languages/count/phil65/mknodes"></a>
<a href="https://github.com/phil65/mknodes"><img alt="Github commits this week" src="https://img.shields.io/github/commit-activity/w/phil65/mknodes"></a>
<a href="https://github.com/phil65/mknodes"><img alt="Github commits this month" src="https://img.shields.io/github/commit-activity/m/phil65/mknodes"></a>
<a href="https://github.com/phil65/mknodes"><img alt="Github commits this year" src="https://img.shields.io/github/commit-activity/y/phil65/mknodes"></a>
<a href="https://codecov.io/gh/phil65/mknodes/"><img alt="Package status" src="https://codecov.io/gh/phil65/mknodes/branch/main/graph/badge.svg"></a>
<a href="https://pyup.io/repos/github/phil65/mknodes/"><img alt="PyUp" src="https://pyup.io/repos/github/phil65/mknodes/shield.svg"></a></p>
<p><a href="https://phil65.github.io/mknodes/">Read the completely coded documentation!</a></p>
<p>The documentation is available in six variants:</p>
<table>
<thead>
<tr>
<th>Variant</th>
<th>Engine / flavour</th>
</tr>
</thead>
<tbody>
<tr>
<td><a href="https://phil65.github.io/mknodes/">Zensical</a></td>
<td><code>--engine zensical</code> (default), Material flavour</td>
</tr>
<tr>
<td><a href="https://phil65.github.io/mknodes/mkdocs/">MkDocs</a></td>
<td><code>--engine mkdocs</code>, Material for MkDocs</td>
</tr>
<tr>
<td><a href="https://phil65.github.io/mknodes/mdbook/">mdBook</a></td>
<td><code>--engine mdbook</code>, mdBook flavour</td>
</tr>
<tr>
<td><a href="https://phil65.github.io/mknodes/sphinx/">Sphinx</a></td>
<td><code>--engine sphinx</code>, MyST flavour (Furo theme)</td>
</tr>
<tr>
<td><a href="https://phil65.github.io/mknodes/docsify/">Docsify</a></td>
<td><code>--engine docsify</code>, GitHub flavour, rendered in the browser</td>
</tr>
<tr>
<td><a href="https://github.com/phil65/mknodes/tree/docs-github">GitHub Markdown</a></td>
<td><code>--engine markdown --flavor github</code>, rendered by GitHub itself</td>
</tr>
</tbody>
</table>
<p>They are all generated from the same build function (<code>mknodes.manual.root:Build.build</code>):
the node tree is written once and each engine gets the Markdown flavour it understands.</p>
<p>MkNodes lets you build documentation from Python objects instead of hand-written Markdown.
Pages, navigation sections and content blocks are <em>nodes</em> that render to
<a href="https://squidfunk.github.io/mkdocs-material/">Material for MkDocs</a>-flavored Markdown and know
which Markdown extensions, CSS and JavaScript they need. Many nodes pull their content directly
from your project (pyproject.toml, git history, installed packages, docstrings), so the docs stay
in sync with the code.</p>
<h2 id="installation">Installation</h2>
<div class="highlight"><pre><span></span><code>pip<span class="w"> </span>install<span class="w"> </span>mknodes
</code></pre></div>
<p>MkNodes includes a site generator (<code>mknodes build</code> / <code>serve</code> / <code>generate</code>) that builds
websites with <a href="https://zensical.org">Zensical</a> (default), <a href="https://www.mkdocs.org">MkDocs</a>
(<code>pip install mknodes[mkdocs]</code>), <a href="https://rust-lang.github.io/mdBook/">mdBook</a> (the <code>mdbook</code>
binary), <a href="https://www.sphinx-doc.org">Sphinx</a> with MyST (<code>pip install mknodes[sphinx]</code>) or
<a href="https://docsify.js.org">Docsify</a> (no build step), or writes plain Markdown. MkDocs is not required. (The generator used to be the
separate <code>mkdocs-mknodes</code> package.)</p>
<h2 id="quickstart">Quickstart</h2>
<div class="highlight"><pre><span></span><code><span class="kn">import</span><span class="w"> </span><span class="nn">mknodes</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="nn">mk</span>

<span class="n">page</span> <span class="o">=</span> <span class="n">mk</span><span class="o">.</span><span class="n">MkPage</span><span class="p">(</span><span class="s2">"Overview"</span><span class="p">)</span>
<span class="n">page</span> <span class="o">+=</span> <span class="n">mk</span><span class="o">.</span><span class="n">MkHeader</span><span class="p">(</span><span class="s2">"My project"</span><span class="p">,</span> <span class="n">level</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
<span class="n">page</span> <span class="o">+=</span> <span class="n">mk</span><span class="o">.</span><span class="n">MkAdmonition</span><span class="p">(</span><span class="s2">"Generated with MkNodes!"</span><span class="p">,</span> <span class="n">typ</span><span class="o">=</span><span class="s2">"tip"</span><span class="p">,</span> <span class="n">title</span><span class="o">=</span><span class="s2">"Note"</span><span class="p">)</span>
<span class="n">page</span> <span class="o">+=</span> <span class="n">mk</span><span class="o">.</span><span class="n">MkTable</span><span class="p">({</span><span class="s2">"Feature"</span><span class="p">:</span> <span class="p">[</span><span class="s2">"Nodes"</span><span class="p">,</span> <span class="s2">"Templating"</span><span class="p">],</span> <span class="s2">"Status"</span><span class="p">:</span> <span class="p">[</span><span class="s2">"yes"</span><span class="p">,</span> <span class="s2">"yes"</span><span class="p">]})</span>
<span class="n">page</span> <span class="o">+=</span> <span class="n">mk</span><span class="o">.</span><span class="n">MkCode</span><span class="p">(</span><span class="s2">"print('hello')"</span><span class="p">,</span> <span class="n">language</span><span class="o">=</span><span class="s2">"py"</span><span class="p">)</span>

<span class="nb">print</span><span class="p">(</span><span class="n">page</span><span class="o">.</span><span class="n">to_markdown_sync</span><span class="p">())</span>  <span class="c1"># async code: `await page.to_markdown()`</span>
<span class="nb">print</span><span class="p">(</span><span class="n">page</span><span class="o">.</span><span class="n">to_markdown_sync</span><span class="p">(</span><span class="n">flavor</span><span class="o">=</span><span class="s2">"github"</span><span class="p">))</span>  <span class="c1"># GitHub flavour (READMEs)</span>
</code></pre></div>
<h2 id="features">Features</h2>
<ul>
<li><strong>75+ nodes</strong>: admonitions, tabs, tables, code blocks, diagrams, cards, timelines, badges, ...</li>
<li><strong>Project-aware nodes</strong>: <code>MkChangelog</code>, <code>MkInstallGuide</code>, <code>MkDependencyTable</code>, <code>MkLicense</code>,
  <code>MkShields</code>, <code>MkClassDiagram</code>, ... build their content from your package and repository.</li>
<li><strong>Navigation trees</strong>: <code>MkNav</code> sections with decorator-based routing, API docs via <code>MkDoc</code>,
  CLI docs via <code>MkCliNav</code>, or parse an existing <code>SUMMARY.md</code> / folder.</li>
<li><strong>Jinja templating</strong>: every node is also a Jinja filter, e.g.
  <code>{{ "Inlined text" | MkAdmonition(inline="left") }}</code>.</li>
<li><strong>Resource tracking</strong>: required Markdown extensions, CSS and JS are collected per page.</li>
<li><strong>Markdown extensions</strong>: <code>/// mknodes</code> blocks and executable Python code blocks
  (<code>mknodes.mdext</code>).</li>
</ul>
<h3 id="build-a-website-tree">Build a website tree</h3>
<div class="highlight"><pre><span></span><code><span class="kn">import</span><span class="w"> </span><span class="nn">mknodes</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="nn">mk</span>


<span class="k">def</span><span class="w"> </span><span class="nf">build</span><span class="p">(</span><span class="n">root</span><span class="p">:</span> <span class="n">mk</span><span class="o">.</span><span class="n">MkNav</span><span class="p">,</span> <span class="n">theme</span><span class="p">:</span> <span class="n">mk</span><span class="o">.</span><span class="n">Theme</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kc">None</span><span class="p">:</span>
    <span class="nd">@root</span><span class="o">.</span><span class="n">route</span><span class="o">.</span><span class="n">page</span><span class="p">(</span><span class="s2">"Home"</span><span class="p">,</span> <span class="n">is_index</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
    <span class="k">def</span><span class="w"> </span><span class="nf">_</span><span class="p">(</span><span class="n">page</span><span class="p">:</span> <span class="n">mk</span><span class="o">.</span><span class="n">MkPage</span><span class="p">):</span>
        <span class="n">page</span> <span class="o">+=</span> <span class="n">mk</span><span class="o">.</span><span class="n">MkHeader</span><span class="p">(</span><span class="s2">"Welcome"</span><span class="p">,</span> <span class="n">level</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
        <span class="n">page</span> <span class="o">+=</span> <span class="n">mk</span><span class="o">.</span><span class="n">MkInstallGuide</span><span class="p">()</span>

    <span class="nd">@root</span><span class="o">.</span><span class="n">route</span><span class="o">.</span><span class="n">page</span><span class="p">(</span><span class="s2">"Changelog"</span><span class="p">,</span> <span class="n">icon</span><span class="o">=</span><span class="s2">"format-list-group"</span><span class="p">)</span>
    <span class="k">def</span><span class="w"> </span><span class="nf">_</span><span class="p">(</span><span class="n">page</span><span class="p">:</span> <span class="n">mk</span><span class="o">.</span><span class="n">MkPage</span><span class="p">):</span>
        <span class="n">page</span> <span class="o">+=</span> <span class="n">mk</span><span class="o">.</span><span class="n">MkChangelog</span><span class="p">()</span>
</code></pre></div>
<p>Configure it in <code>pyproject.toml</code> and build the website (run inside your project's repository):</p>
<div class="highlight"><pre><span></span><code><span class="k">[tool.mknodes]</span>
<span class="n">build-fn</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"mypackage.docs:build"</span>
</code></pre></div>
<div class="highlight"><pre><span></span><code>mknodes<span class="w"> </span>build<span class="w">                      </span><span class="c1"># pages + mkdocs.generated.yml, then `zensical build`</span>
mknodes<span class="w"> </span>serve<span class="w">                      </span><span class="c1"># live preview</span>
mknodes<span class="w"> </span>build<span class="w"> </span>--engine<span class="w"> </span>mkdocs<span class="w">      </span><span class="c1"># same input, built with MkDocs + Material</span>
mknodes<span class="w"> </span>build<span class="w"> </span>--engine<span class="w"> </span>mdbook<span class="w">      </span><span class="c1"># book/ (book.toml, src/SUMMARY.md), then `mdbook build`</span>
mknodes<span class="w"> </span>build<span class="w"> </span>--engine<span class="w"> </span>sphinx<span class="w">      </span><span class="c1"># sphinx/ (conf.py, MyST pages, toctrees), then `sphinx-build`</span>
mknodes<span class="w"> </span>build<span class="w"> </span>--engine<span class="w"> </span>docsify<span class="w">     </span><span class="c1"># site/ (index.html, _sidebar.md, pages): no build step</span>
mknodes<span class="w"> </span>generate<span class="w"> </span>--engine<span class="w"> </span>markdown<span class="w"> </span>--flavor<span class="w"> </span>github<span class="w"> </span>--generated-dir<span class="w"> </span>out<span class="w">  </span><span class="c1"># plain Markdown</span>
</code></pre></div>
<p>Each engine renders its own Markdown flavour (<code>material</code> for Zensical / MkDocs, <code>mdbook</code>,
<code>myst</code> for Sphinx, <code>github</code> for Docsify and plain Markdown): admonitions, tabs, code blocks and API docs degrade to what the
target understands. See <a href="https://phil65.github.io/mknodes/Site%20generator/site_engines/">Site engines</a>.</p>
<p>Without a build function, a default website gets generated (README, API docs, CLI docs,
development pages). Build functions are called as <code>build_fn(root=..., theme=..., **kwargs)</code>
(sync or async) and populate <code>root</code>.</p>
<p>The CLI also renders Jinja-enhanced Markdown: <code>mknodes render -i template.md -o output.md</code>
and <code>mknodes render-folder -i src_docs -o docs</code>.</p>
<h3 id="markdown-extensions">Markdown extensions</h3>
<div class="highlight"><pre><span></span><code><span class="kn">import</span><span class="w"> </span><span class="nn">markdown</span>

<span class="kn">from</span><span class="w"> </span><span class="nn">mknodes.mdext.execute_ext</span><span class="w"> </span><span class="kn">import</span> <span class="n">get_custom_fences</span>

<span class="n">md</span> <span class="o">=</span> <span class="n">markdown</span><span class="o">.</span><span class="n">Markdown</span><span class="p">(</span>
    <span class="n">extensions</span><span class="o">=</span><span class="p">[</span><span class="s2">"pymdownx.superfences"</span><span class="p">,</span> <span class="s2">"mknodes.mdext"</span><span class="p">],</span>
    <span class="n">extension_configs</span><span class="o">=</span><span class="p">{</span><span class="s2">"pymdownx.superfences"</span><span class="p">:</span> <span class="p">{</span><span class="s2">"custom_fences"</span><span class="p">:</span> <span class="n">get_custom_fences</span><span class="p">()}},</span>
<span class="p">)</span>
</code></pre></div>
<div class="highlight"><pre><span></span><code>/// mknodes
{{ "Dynamic Header" | MkHeader(level=2) }}
///

<span class="sb">```python</span><span class="w"> </span>exec="true"
<span class="nb">print</span><span class="p">(</span><span class="s2">"**Generated at build time!**"</span><span class="p">)</span>
<span class="sb">```</span>
</code></pre></div>
<p>Use <code>session="name"</code> on <code>exec</code> blocks to share variables between them. See
<a href="https://github.com/phil65/mknodes/blob/main/src/mknodes/mdext/README.md"><code>src/mknodes/mdext/README.md</code></a> for all options.</p>
<h2 id="documentation">Documentation</h2>
<p>The <a href="https://phil65.github.io/mknodes/">documentation</a> is itself built with MkNodes
(see <code>src/mknodes/manual/</code>) and shows every node with examples.</p>

Bases: MkText

__init__

__init__(target: str | PathLike[str] | MkText, **kwargs: Any) -> None

Parameters:

Name Type Description Default
target str | PathLike[str] | MkText

target to include

required
kwargs Any

Keyword arguments passed to parent

{}
Name Children Inherits
MkText
mknodes.basenodes.mktext
Class for any Markup text.
graph TD
  2309987051["mkinclude.MkInclude"]
  1315922923["mktext.MkText"]
  3160803176["rendercache.TemplateRenderMixin"]
  2751680898["builtins.object"]
  1359323199["mknode.MkNode"]
  1315922923 --> 2309987051
  3160803176 --> 1315922923
  2751680898 --> 3160803176
  1359323199 --> 1315922923
  2751680898 --> 1359323199
/home/runner/work/mknodes/mknodes/src/mknodes/templatenodes/mkinclude/metadata.toml
[metadata]
name = "MkInclude"
icon = "octicon:file-symlink-file-24"

[examples.url]
title = "Include from URL"
jinja = """
{{ "https://raw.githubusercontent.com/phil65/mknodes/main/README.md" | MkInclude }}
"""

[output.markdown]
template = """
{{ node.get_text() }}
"""
mknodes.templatenodes.mkinclude.MkInclude
class MkInclude(mktext.MkText):
    """Node to include the text of other Markdown files / MkNodes.

    This node only keeps a reference to given target and resolves it when needed.
    Target can either be an URL or any other fsspec protocol path to a markdown file,
    a file system path to a markdown file, or another MkNode.
    """

    ICON = "octicons/file-symlink-file-24"

    def __init__(self, target: str | os.PathLike[str] | mktext.MkText, **kwargs: Any) -> None:
        """Constructor.

        Args:
            target: target to include
            kwargs: Keyword arguments passed to parent
        """
        super().__init__(**kwargs)
        self.target = target

    def _remote_url(self) -> str | None:
        """Return the target if it is a remote URL (pinned in the lock file)."""
        if isinstance(self.target, str) and pathhelpers.is_remote(self.target):
            return self.target
        return None

    def get_input_requests(self) -> list[inputs.InputRequest]:
        if url := self._remote_url():
            return [inputs.InputRequest.create("file", url=url)]
        return []

    def get_text(self) -> str:
        if url := self._remote_url():
            return self.ctx.inputs.get("file", url=url)
        match self.target:
            case os.PathLike() | str():
                return pathhelpers.load_file_cached(os.fspath(self.target))
            case mktext.MkText():
                return str(self.target)
            case _:
                raise TypeError(self.target)